/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

package testing;
import lime2.*;
import lights.adapters.*;
import lights.interfaces.*;
import java.util.Random;

/**
 * A simple agent installing a (once) strong reaction which triggers on template <code>hello</code>: the reaction 
 * removes <code>hello</code> and installs another (once) strong reaction which triggers on template 
 * <code>String</code>; the firing of the second reaction removes any <code>String</code> tuple and outs the tuple 
 * <code>done</code>.
 *   
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class Inst2React implements Runnable, ReactionListener 
{
 // identification number	
 private int id = new Random().nextInt(1000);		
	
 // private data members
 private LimeTupleSpace lts = LimeTupleSpace.get("testing");	
 private ITuple strings = new Tuple().add(new Field().setType(String.class));
 private LimeTemplate template1 = new LimeTemplate(Location.HERE, Location.UNSPECIFIED, strings);
 private ITuple hello = new Tuple().add(new Field().setValue("hello"));
 private LimeTemplate template2 = new LimeTemplate(Location.HERE, Location.UNSPECIFIED, hello);
 
 /**
  * Run method.
  */
 public void run()
 {
  System.out.println("Inst2React " + id + ": started");   
  Reaction r = new Reaction(Reaction.ONCE, template2, this);
  lts.addStrongReaction(new Reaction[] {r});
  System.out.println("Inst2React " + id + ": 1st reaction installed");   
  System.out.println("Inst2React " + id + ": dies");
 }
 
 /**
  * Reactive method.
  * 
  * @param re ReactionEvent
  */
 public void reactsToOut(ReactionEvent re)
 {
  System.out.println("Inst2React " + id + ": 1st reaction fires");	
  Reaction r = new Reaction(Reaction.ONCE, template1, new SecondRListener(id, lts));
  lts.inp(template2);
  lts.addStrongReaction(new Reaction[] {r});
  System.out.println("Inst2React " + id + ": 1st reaction installed 2nd reaction");	
 }
 
 /**
  * Another reaction Listener. 
  */
 class SecondRListener implements ReactionListener
 {		
  // private member variables
  private int id2;
  private LimeTupleSpace lts2;	
 	
  /**
   * Constructor method.
   * 
   * @param lts2 the LimeTupleSpace of the installer
   */	
  public SecondRListener(int id2, LimeTupleSpace lts2)
  {
   this.id2 = id2;
   this.lts2 = lts2;	
  }
 	
  /**
   * Reactive method.
   * 
   * @param re ReactionEvent
   */
  public void reactsToOut(ReactionEvent re)
  {
   System.out.println("Inst2React " + id + ": 2nd reaction fires");	
   lts2.ing(template1);
   lts2.out(new Tuple().add(new Field().setValue("done")));
  }

@Override
public void reactsToTake(ReactionEvent re) {
	// TODO Auto-generated method stub
	
}
  
 }

@Override
public void reactsToTake(ReactionEvent re) {
	// TODO Auto-generated method stub
	
}
 
}

